package net.mcreator.infinite_storage.procedure;

import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.world.World;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;

import net.mcreator.infinite_storage.InfiniteStorageMod;
import net.mcreator.infinite_storage.EntityDataMsgClientInfiniteStorageMod;
import net.mcreator.infinite_storage.ElementsInfiniteStorageMod;

import java.util.Map;
import java.util.HashMap;

@ElementsInfiniteStorageMod.ModElement.Tag
public class ProcedurePlayerUpdate extends ElementsInfiniteStorageMod.ModElement {
	public ProcedurePlayerUpdate(ElementsInfiniteStorageMod instance) {
		super(instance, 35);
	}
	static Map<String, Object> $_dependencies = new HashMap<>();
	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			return;
		}
		if (dependencies.get("world") == null) {
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		World world = (World) dependencies.get("world");
		NBTTagCompound InfiniteBag = entity.getEntityData().getCompoundTag("InfiniteBag");
		if (((world.getWorldTime() % 10) == 0)) {
			if (entity instanceof EntityLivingBase && !entity.world.isRemote) {
				EntityLivingBase living = (EntityLivingBase) entity;
				if (living instanceof EntityPlayer) {
					EntityPlayerMP playerMP = (EntityPlayerMP) living;
					EntityPlayer player = (EntityPlayer) living;
					NBTTagCompound dataToSendToClient = player.getEntityData();
					InfiniteStorageMod.NETWORK_WRAPPER_CLIENT.sendTo(new EntityDataMsgClientInfiniteStorageMod(dataToSendToClient),
							(EntityPlayerMP) entity);
				}
			}
		}
		if ((!((InfiniteBag.getString("InfiniteBagTip")).equals("")))) {
			InfiniteBag.setDouble("InfiniteBagTipClear", (InfiniteBag.getDouble("InfiniteBagTipClear") + 1));
			if ((InfiniteBag.getDouble("InfiniteBagTipClear") >= 30)) {
				InfiniteBag.setString("InfiniteBagTip", "");
				InfiniteBag.setDouble("InfiniteBagTipClear", 0);
			}
			entity.getEntityData().setTag("InfiniteBag", InfiniteBag);
		}
	}

	@SubscribeEvent
	public void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Entity entity = event.player;
			World world = entity.world;
			int i = (int) entity.posX;
			int j = (int) entity.posY;
			int k = (int) entity.posZ;
			java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
			dependencies.put("x", i);
			dependencies.put("y", j);
			dependencies.put("z", k);
			dependencies.put("world", world);
			dependencies.put("entity", entity);
			dependencies.put("event", event);
			this.executeProcedure(dependencies);
		}
	}

	@Override
	public void preInit(FMLPreInitializationEvent event) {
		MinecraftForge.EVENT_BUS.register(this);
	}
}
